package com.example.studentstatusmanagement.service;

import com.example.studentstatusmanagement.mapper.BuildingMapper;
import com.example.studentstatusmanagement.mapper.DormMapper;
import com.example.studentstatusmanagement.mapper.PlotMapper;
import com.example.studentstatusmanagement.pojo.Building;
import com.example.studentstatusmanagement.pojo.Dorm;
import com.example.studentstatusmanagement.pojo.DormExample;
import com.example.studentstatusmanagement.pojo.Plot;
import com.example.studentstatusmanagement.pojo.doo.ReqRoom;
import com.example.studentstatusmanagement.pojo.dto.BuildingInfo;
import com.example.studentstatusmanagement.pojo.dto.DormInfo;
import com.example.studentstatusmanagement.pojo.dto.PlotInfo;
import com.example.studentstatusmanagement.pojo.dto.SRoom;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DormServiceImpl implements DormService {

    @Resource
    private DormMapper dormMapper;

    @Resource
    private PlotMapper plotMapper;

    @Resource
    private BuildingMapper buildingMapper;

    // 返回三级联动宿舍情况
    @Override
    public List<BuildingInfo> selectDormInfos() {

        return selectBuildings();

    }

    @Override
    public int insertSelective(Dorm record) {
        return dormMapper.insertSelective(record);
    }

    @Override
    public List<Dorm> selectByExample(DormExample example) {
        return dormMapper.selectByExample(example);
    }

    @Override
    public Dorm selectByPrimaryKey(Integer id) {
        return dormMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Dorm record) {
        return dormMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<ReqRoom> selectPlotsForDrom() {
        return dormMapper.selectPlotsForDrom();
    }

    @Override
    public List<SRoom> selectDorms(ReqRoom reqRoom) {
        return dormMapper.selectDorms(reqRoom);
    }

    // 查询校区
    private List<BuildingInfo> selectBuildings() {

        List<BuildingInfo> buildingInfos = new ArrayList<>();

        for (int i = 0; i < buildingMapper.selectBuildings().size(); i++) {

            BuildingInfo buildingInfo = buildingMapper.selectBuildings().get(i);
            buildingInfo.setChildren(selectPlots(buildingInfo.getId()));
            buildingInfos.add(buildingInfo);
        }
        return buildingInfos;
    }

    // 查询宿舍区
    private List<PlotInfo> selectPlots(Integer id) {

        List<PlotInfo> plotInfos = new ArrayList<>();

        for (int i = 0; i < plotMapper.selectPlotsByBuildingId(id).size(); i++) {

            PlotInfo plotInfo = plotMapper.selectPlotsByBuildingId(id).get(i);
            plotInfo.setChildren(selectDorms(plotInfo.getId()));

            plotInfos.add(plotInfo);
        }
        return plotInfos;
    }

    //查询宿舍号
    private List<DormInfo> selectDorms(Integer id) {

        return dormMapper.selectDormsByPlotId(id);
    }
}
